package com.senlingspace.payment.lakala.service.impl;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Joiner;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.V3LabsRelationRefundRequest;
import com.lkl.laop.sdk.request.model.V3LabsTradeLocationInfo;
import com.senlingspace.common.core.dingtalk.DingTalkService;
import com.senlingspace.common.exception.payment.LakalaException;
import com.senlingspace.payment.config.properties.LakalaProperties;
import com.senlingspace.payment.lakala.bean.request.LakalaRefundRequest;
import com.senlingspace.payment.lakala.bean.response.LakalaRefundResponse;
import com.senlingspace.payment.lakala.service.LakalaRefundService;
import com.senlingspace.system.domain.SlkjMerchant;
import com.senlingspace.system.mapper.SlkjMerchantMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 退款
 *
 * <a href="https://o.lakala.com/#/home/document/detail?id=113">退款</a>
 *
 * @author lebronjames
 * @date 2024/07/24
 */
@AllArgsConstructor
@Slf4j
@Service
public class LakalaRefundServiceImpl implements LakalaRefundService {

    private final LakalaProperties lakalaProperties;
    private final DingTalkService dingTalkService;

    private final SlkjMerchantMapper slkjMerchantMapper;


    @Override
    public LakalaRefundResponse refund(LakalaRefundRequest refundRequest) throws LakalaException {
        log.info("退款请求参数：{}", JSON.toJSONString(refundRequest));
        if (Objects.isNull(refundRequest)) {
            log.error("退款请求参数不能为空");
            throw new LakalaException("退款请求参数不能为空");
        }

        if (StringUtils.isBlank(refundRequest.getLogNo())) {
            log.error("退款请求参数logNo不能为空");
            throw new LakalaException("退款请求参数logNo不能为空");
        }

        if (StringUtils.isBlank(refundRequest.getOutTradeNo())) {
            log.error("退款请求参数outTradeNo不能为空");
            throw new LakalaException("退款请求参数outTradeNo不能为空");
        }

        if (StringUtils.isBlank(refundRequest.getRefundAmount())) {
            log.error("退款金额不能为空");
            throw new LakalaException("退款金额不能为空");
        }
        if (refundRequest.getRefundAmount().contains(".")) {
            log.error("退款金额必须转为分");
            throw new LakalaException("退款金额必须转为分");
        }

        SlkjMerchant merchant = slkjMerchantMapper.selectByShopId(refundRequest.getShopId());
        if (Objects.isNull(merchant) || StringUtils.isBlank(merchant.getTermNo()) || StringUtils.isBlank(merchant.getMerchantNo())) {
            log.error("门店 [{}] 商户信息配置有误！", refundRequest.getShopId());
            String logMessage = Joiner.on("").join("门店 [", refundRequest.getShopId(), "] 商户信息配置有误，请检查 [slkj_merchant]！");
            dingTalkService.sendTextMessage(logMessage, true);
            throw new LakalaException("门店商户信息有误！");
        }

        V3LabsRelationRefundRequest request = new V3LabsRelationRefundRequest();
        request.setRefundAmount(refundRequest.getRefundAmount());
        request.setOriginLogNo(refundRequest.getLogNo());
        request.setRefundReason(refundRequest.getRefundReason());
        request.setOutTradeNo(refundRequest.getOutTradeNo());
        request.setMerchantNo(merchant.getMerchantNo());
        request.setTermNo(merchant.getTermNo());
        request.setLocationInfo(new V3LabsTradeLocationInfo(lakalaProperties.getServerIpAddress()));
        log.info("拉卡拉退款请求参数：{}", JSON.toJSONString(request));
        try {
            String json = LKLSDK.httpPost(request);
            log.info("拉卡拉退款返回结果：{}", json);
            if (StringUtils.isBlank(json)) {
                log.error("退款失败，拉卡拉返回数据异常");
                dingTalkService.sendTextMessage("退款失败，拉卡拉返回数据为空！" + "，对应入参：" + JSON.toJSONString(request), true);
                return null;
            }
            return JSON.parseObject(json, LakalaRefundResponse.class);
        } catch (SDKException e) {
            log.error("退款失败，拉卡拉系统异常：{}", e.getMessage());
            dingTalkService.sendTextMessage("退款失败，拉卡拉系统异常：" + e.getMessage() + "，对应请求入参：" + JSON.toJSONString(request), true);
            return null;
        }
    }

}
